package hot100;

public class maxSubArray2 {
    public int maxSubArray2(int[] nums) {
        int max = Integer.MIN_VALUE;
        int preSum = 0;
        for(int x : nums) {
            preSum = Math.max(x,preSum + x);
            max = Math.max(max,preSum);
        }
        return max;
    }
    public int maxSubArray(int[] nums) {
        //滑动窗口
        int left= 0;
        int right = 0;
        int sum = 0;
        int max = Integer.MIN_VALUE;
        //进窗口
        while(right < nums.length) {
            sum += nums[right];

            if(max < sum) {
                max = sum;
            }
            //sum < 0，出窗口
            if(sum < 0) {
                sum = 0;
            }


            right++;
        }
        return max;


    }
}
